package com.java.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.common.model.Result;
import com.java.common.util.JwtUtil;
import com.java.manage.dao.AccessoriesMapper;
import com.java.manage.dao.InventoryMapper;
import com.java.manage.model.Accessories;
import com.java.manage.model.Inventory;
import com.java.manage.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//库存管理
@Service
public class InventoryServiceImpl implements InventoryService {
    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private AccessoriesMapper accessoriesMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public Result save(Inventory inventory) {
        inventoryMapper.save(inventory);
        return Result.success();
    }
    @Override
    public Result modify(Inventory inventory) {
        inventoryMapper.modify(inventory);
        Accessories accessories = inventory.getAccessories();
        accessories.setId(inventory.getAccessoriesId());
        accessoriesMapper.modify(accessories);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            inventoryMapper.remove(id);
        }
        return Result.success();
    }
    @Override
    public Result list(Inventory inventory) {
        PageHelper.startPage(inventory.getPageNum(), inventory.getPageSize());

        String token = request.getHeader("Token");
        Integer storeId = (Integer) JwtUtil.getClaims(token).get("storeId");
        inventory.setStoreId(storeId);

        List<Inventory> list = inventoryMapper.list(inventory);
        PageInfo<Inventory> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }


    @Override
    public Result get(int id) {
        return null;
    }

    @Override
    public Result lists(Integer id) {
        List<Inventory> list = inventoryMapper.lists(id);
        PageInfo<Inventory> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }
}
